<?php
// +----------------------------------------------------------------------
// | 店铺店员
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Validate;

class ShopStaff extends Model
{
	
	//隐藏字段
	protected $hidden = ["is_del",'is_good','password','update_time','add_time','sort_no','code'];

	//评论，一对一关联
	public function shopInfo(){
        return $this->hasOne('shop','id','shop_id');
    }

	//评论，一对一关联
	public function shopRules(){
        return $this->hasOne('shopRoles','id','roles_id')->bind([
        	'roles_name'=>'name'
        ]);
    }
    
  	//关联预约订单，一对多关联
	public function yyOrder(){
        return $this->hasMany('shopYyOrder','staff_id');
	}
    
    //关联评价内容，一对多关联
	public function evaluate(){
        return $this->hasMany('ShopStaffEvaluate','staff_id');
	}
	
	/**
     * 获取店员列表
     * @param type $shopId
     * @return type
     */
	public function getList($shopId)
    {
    	$model = new self;
		$model = $model->where("shop_id",$shopId);
		$model = $model->where("is_del",0);
		$model = $model->where("status",1);
		$model = $model->field("id,gh,name,img,work_type");
		$model = $model->order("is_good","desc");
		$model = $model->order("add_time","desc");
		return $model->select();
    }
	
	/**
     * 添加店员
     * @param type $data
     * @return type
     */
	public function add($data)
    {
    	$validate = new Validate;
		$validate->rule([
			'shop_id'=>'require',
			'roles_id'=>'require',
        	'name' => 'require|length:2,5',
        	'phone' => 'require|mobile|unique:shop_staff,shop_id^phone',
		]);
		$validate->message ([
			'code.unique'=>'该员工账号已存在！',
			'phone.unique'=>'该手机号或员工已存在！',
			'phone.mobile'=>'请输入正确的手机号码！',
		]);
        if( !$validate->check($data) ) {
        	throw new \think\Exception($validate->getError());
        }
    	self::allowField(true)  -> save($data);
		return $this;
    }
	
	/**
     * 编辑店员
     * @param type $data
     * @return type
     */
	public function edit($data)
    {
    	$validate = new Validate;
		$validate->rule([
			'id'=>'require',
			'roles_id'=>'require',
        	'name' => 'require|length:2,5',
        	'phone' => 'require|mobile|unique:shop_staff,phone,'.$data['id'].',id',
        	'status' => 'require|in:0,1',
		]);
		$validate->message ([
			'phone.unique'=>'该手机号或员工已存在！',
			'phone.mobile'=>'请输入正确的手机号码！',
		]);
        if( !$validate->check($data) ) {
        	throw new \think\Exception($validate->getError());
        }
    	self::allowField(true)->isUpdate(true) -> save($data);
		return $this;
    }
	
	
	/**
     * 删除店员
     * @param type $id
     * @return type
     */
	public function del($shopId,$id)
    {
    	return self::where('shop_id',$shopId)->where('id',$id) ->delete();
    }
    
    /**
     * 绑定会员
     * @param type $id
     * @return type
     */
	public function bindingMember($id,$uid)
    {
    	return self::where('id',$id)->update(['uid'=>$uid]);
    }
    
	
}
